/* *****************************************************************************************************
 * Copyright 2009. Dbwebx Group
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 * *****************************************************************************************************
 */

package org.dbwebx.persistence;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * The entity represents database source code unit e.g. SQL file containing some
 * SQL code.
 * 
 * @author Izzet_Mustafayev
 */
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "t_dbunits")
public class DBUnit implements Serializable {
	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = 14905138401876738L;

	/**
	 * The {@link DBUnit}'s identity.
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id")
	protected Long identity;

	/**
	 * The {@link DBUnit}'s name.
	 */
	protected String name;

	/**
	 * The {@link DBUnit}'s {@link Bundle}
	 */
	@ManyToOne
	@JoinColumn(name = "bundle_id", nullable = false)
	protected Bundle bundle;

	/**
	 * The {@link DBUnit}'s last modifier user.
	 */
	@ManyToOne
	@JoinColumn(name = "modifier_id")
	protected User modifier;

	/**
	 * The {@link DBUnit}'s modification time.
	 */
	@Column(name = "updated_on")
	@Temporal(TemporalType.TIMESTAMP)
	protected Date updatedOn = new Date();

	/**
	 * @return the identity
	 */
	public Long getIdentity() {
		return identity;
	}

	/**
	 * @param identity
	 *            the identity to set
	 */
	public void setIdentity(final Long identity) {
		this.identity = identity;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(final String name) {
		this.name = name;
	}

	/**
	 * @return the bundle
	 */
	public Bundle getBundle() {
		return bundle;
	}

	/**
	 * @param bundle
	 *            the bundle to set
	 */
	public void setBundle(final Bundle bundle) {
		this.bundle = bundle;
	}

	/**
	 * @return the modifier
	 */
	public User getModifier() {
		return modifier;
	}

	/**
	 * @param modifier
	 *            the modifier to set
	 */
	public void setModifier(final User modifier) {
		this.modifier = modifier;
	}

	/**
	 * @return the updatedOn
	 */
	public Date getUpdatedOn() {
		return updatedOn;
	}

	/**
	 * @param updatedOn
	 *            the updatedOn to set
	 */
	public void setUpdatedOn(final Date updatedOn) {
		this.updatedOn = updatedOn;
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this).append(identity).append(name)
				.toString();
	}

	@Override
	public boolean equals(final Object obj) {
		if (obj instanceof DBUnit == false) {
			return false;
		}

		if (this == obj) {
			return true;
		}

		DBUnit rhs = (DBUnit) obj;

		return new EqualsBuilder().append(identity, rhs.identity).append(name,
				rhs.name).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(11, 49).append(identity).append(name)
				.toHashCode();
	}
}
